/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import profile.hujia.redis.service.PutThread;
import profile.hujia.redis.service.RedisTest;
import profile.hujia.redis.service.WorkItemBuild;

import java.util.concurrent.CountDownLatch;

/**
 * class info
 *
 * @author hujia
 * @date 2018/11/9 9:44
 */
@Slf4j
@RestController
public class JedisPoolController {

    private RedisTest redisTest;
    private WorkItemBuild workItemBuild;

    public JedisPoolController(@Qualifier("jedisPoolRedisTest") RedisTest redisTest, WorkItemBuild workItemBuild) {
        this.redisTest = redisTest;
        this.workItemBuild = workItemBuild;
    }

    @GetMapping("/jedis/pool/thread/put")
    public void threadTest(@RequestParam Integer times) {
        StopWatch stopWatch=new StopWatch("jedis-pool");
        stopWatch.start("多线程存redis");
        CountDownLatch latch = new CountDownLatch(times);
        for (int i = 0; i < times; ++i) {
            PutThread putThread = new PutThread(redisTest, workItemBuild, 100, latch);
            putThread.run();
        }
        try {
            latch.await();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        stopWatch.stop();
        log.debug(stopWatch.prettyPrint());
    }


}
